package com.woniu.cms.settlement.core.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.cms.settlement.core.entity.SettlementExpenses;
import com.woniu.cms.settlement.core.entity.SettlementInformation;
import com.woniu.cms.settlement.core.mapper.SettlementExpensesMapper;
import com.woniu.cms.settlement.core.service.impl.SettlementExpensesServiceImpl;
import com.woniu.cms.settlement.core.service.impl.SettlementInformationServiceImpl;
import com.woniu.common.api.Result;
import com.woniu.common.api.status.IStatusCode;
import com.woniu.common.api.status.Impl.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * <p>
 * 合同，预控，财务费用 前端控制器
 * </p>
 *
 * @author romantic sheep
 * @since 2022-09-20
 */
@RestController
@RequestMapping("/settlement-expenses")
@Api(tags = "合同，预控，财务费用")
public class SettlementExpensesController {
    @Autowired
    private SettlementExpensesServiceImpl expensesService;

    @Autowired
    private SettlementExpensesMapper settlementExpensesMapper;
    @Autowired
    private SettlementInformationServiceImpl informationService;


    @PostMapping("/modify/{id}")
    @ApiOperation("合同总金额,预控总金额")
    public Result modify(@PathVariable("id") Integer id) {
        Integer modify = expensesService.modify(id);
        return Result.common(modify);
    }

    @PostMapping ("/renew")
    @ApiOperation("合同，预控，财务费用修改新增")
    public Result renew(@RequestBody SettlementExpenses settlementExpenses){

        QueryWrapper<SettlementInformation> qw = new QueryWrapper<>();
        qw.eq("project_name", settlementExpenses.getProjectName());

        SettlementInformation one = informationService.getOne(qw);
        Optional<SettlementInformation> one1 = Optional.ofNullable(one);
        settlementExpenses.setInforId(one1.get().getId());

        List<SettlementExpenses> list = expensesService.list();
        for (SettlementExpenses expenses : list) {
            if (expenses.getProjectName().equals(settlementExpenses.getProjectName())){
                return Result.error(ResultCode.USER_HAS_EXISTED,settlementExpenses);
            }
        }
        boolean save = expensesService.save(settlementExpenses);
        return Result.common(save);
    }

    @PutMapping("{id}")
    @ApiOperation("删除")
    public Result de(@PathVariable Integer id){
        int i = settlementExpensesMapper.deleteById(id);
        return Result.common(i);
    }

}

